package com.lyl.momanapp.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.adapter.CiMaAdapter;
import com.lyl.momanapp.adapter.ColorAdapter;
import com.lyl.momanapp.bean.ProductTtemp;
import com.lyl.momanapp.bean.SendCaptcha;
import com.lyl.momanapp.bean.ShopColor;
import com.lyl.momanapp.bean.ShopSize;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.bean.Upimg;
import com.lyl.momanapp.frament.OneFour;
import com.lyl.momanapp.frament.OneOne;
import com.lyl.momanapp.frament.OneThree;
import com.lyl.momanapp.frament.OneThree1;
import com.lyl.momanapp.frament.OneTwo;
import com.lyl.momanapp.util.utlis.AppCommond;
import com.lyl.momanapp.util.utlis.FragmentController;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyBaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.lyl.momanapp.bean.URL.ID_shop_size;
import static com.lyl.momanapp.bean.URL.Userlogin;
import static com.lyl.momanapp.bean.URL.productTemplates;
import static com.lyl.momanapp.bean.URL.sejiandshopURL;
import static com.lyl.momanapp.bean.URL.shopColor;
import static com.lyl.momanapp.bean.URL.shopnum;
import static com.lyl.momanapp.bean.URL.shopsize;

/**
 * 名字 梁亚龙
 * 时间 2017/9/9.
 * QQ邮箱 503934353@qq.com
 */

public class OneActivity extends MyBaseActivity {

    private int num = 0;
    private Button mOneBut;
    private RadioGroup mRG;
    private List<Fragment> mListFragment = new ArrayList<>();
    private FragmentController mController;
    private ImageView mFanhui;
    private ImageView mZhuye;
    private Dialog dialog;
    private View inflate;
    private TextView mShopCar, mLiJI;
    String tag = "";
    private OneTwo oneTwo;
    private OneThree1 oneThree1;
    private RecyclerView mCiMa;
    private CiMaAdapter ciMaAdapter;
    private boolean state = true;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {


            switch (msg.what) {
                case ID_shop_size: {
                    ShopSize shopSize = new Gson().fromJson(msg.obj.toString(), ShopSize.class);
                    ciMaAdapter.showData((ArrayList<String>) shopSize.getResult());
                }
                break;
                case URL.ID_shop_color: {
                    ShopColor shopColor = new Gson().fromJson(msg.obj.toString(), ShopColor.class);
                    colorAdapter.showData((ArrayList<ShopColor.ResultBean>) shopColor.getResult());
                }
                break;
                case URL.ID_img_up: {
                    Upimg upimg = new Gson().fromJson(msg.obj.toString(), Upimg.class);
                    HashMap<String, String> map = new HashMap<>();
//                    map.put("user_id", Userlogin.getResult().getUser_id() + "");
//                    map.put("product_temp_id", productTemplates.getProduct_temp_id() + "");
//                    map.put("product_image", upimg.getResult().getOriginal_image());
//                    map.put("price", productTemplates.getDefault_price());
//                    new MyUtils().xUtilsPost(URL.URL_product, map, handler, URL.ID_product);
//                    HashMap<String, String> map = new HashMap<>();
                    map.put("user_id", Userlogin.getResult().getUser_id() + "");
                    map.put("product_id", "2017120410198100");
                    map.put("colour_name", shopColor);
                    map.put("size_name", shopsize);
                    map.put("quantity", shopnum);
                    new MyUtils().xUtilsPost(URL.URLshopping, map, handler, URL.ID_shopping);
                }
                break;
                case URL.ID_product: {
                    ProductTtemp productTtemp = new Gson().fromJson(msg.obj.toString(), ProductTtemp.class);
                    //                    Log.i("111111", "handleMessage: "+msg.obj.toString());
                    HashMap<String, String> map = new HashMap<>();
                    map.put("user_id", Userlogin.getResult().getUser_id() + "");
                    map.put("product_id", productTtemp.getProduct_id());
                    map.put("colour_name", shopColor);
                    map.put("size_name", shopsize);
                    map.put("quantity", shopnum);
                    new MyUtils().xUtilsPost(URL.URLshopping, map, handler, URL.ID_shopping);
                }
                break;
                case URL.ID_shopping: {
                    SendCaptcha sendCaptcha = new Gson().fromJson(msg.obj.toString(), SendCaptcha.class);
                    if (sendCaptcha.getState().equals("M00000")) {
                        Toast.makeText(OneActivity.this, sendCaptcha.getMessage(), Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(OneActivity.this, ShopCarActivity.class));
                    } else {
                        Toast.makeText(OneActivity.this, sendCaptcha.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            }
        }
    };
    private RecyclerView mColor;
    private ColorAdapter colorAdapter;
    private ImageView mShopJia;
    private ImageView mShopJian;
    private TextView mShopNum;
    private TextView mShopname;
    private TextView mShopjage;

    @Override
    public void initUI() {
        setContentView(R.layout.activity_one);
        mOneBut = ((Button) findViewById(R.id.one_but));
        mRG = ((RadioGroup) findViewById(R.id.one_radio_group));
        mFanhui = ((ImageView) findViewById(R.id.fanhui));
        mZhuye = ((ImageView) findViewById(R.id.zhuye));

    }

    @Override
    public void initData() {
        oneTwo = new OneTwo();
        oneThree1 = new OneThree1();
        mListFragment.add(new OneOne());
        mListFragment.add(oneTwo);
        mListFragment.add(new OneThree());
        mListFragment.add(oneThree1);
        mListFragment.add(new OneFour());

        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null) {
            //接收name值
            tag = bundle.getString("tag");
        }
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @Override
    public void addListeners() {
        mFanhui.setOnClickListener(this);
        mZhuye.setOnClickListener(this);
        mOneBut.setOnClickListener(this);
        mController = FragmentController.getInstance(R.id.one_fl, this.getSupportFragmentManager(), (ArrayList<Fragment>) mListFragment);
        mController.showFragment(0);
        if (tag.equals("OK")) {
            num = 2;
            mRG.check(R.id.one_rb_3);
            mController.showFragment(2);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one_but: {
                num++;
                if (num == 0) {
                    mRG.check(R.id.one_rb_1);
                    mController.showFragment(0);
                } else if (num == 1) {

                    mRG.check(R.id.one_rb_2);
                    mController.showFragment(1);
                    oneTwo.shuaxin();
                } else if (num == 2) {
                    oneTwo.seji();
                    mRG.check(R.id.one_rb_3);
                    mController.showFragment(2);
                } else if (num == 3) {
                    if (productTemplates != null) {
                        mController.showFragment(3);
                    } else {
                        num--;
                        Toast.makeText(OneActivity.this, "请选择商品", Toast.LENGTH_SHORT).show();
                    }
                } else if (num == 4) {
                    oneThree1.baocun();
                    mRG.check(R.id.one_rb_4);
                    mController.showFragment(4);
                    show();

                } else if (num == 5) {

                    startActivity(new Intent(OneActivity.this, UserOrderXiaActivity.class));
                    OneActivity.this.finish();

                }
            }
            break;
            case R.id.fanhui: {
                num--;
                if (num == 0) {
                    mRG.check(R.id.one_rb_1);
                    mController.showFragment(0);
                } else if (num == 1) {
                    mRG.check(R.id.one_rb_2);
                    mController.showFragment(1);
                } else if (num == 2) {
                    mRG.check(R.id.one_rb_3);
                    mController.showFragment(2);
                } else if (num == 3) {
                    if (productTemplates != null) {
                        mController.showFragment(3);
                    } else {
                        num--;
                        Toast.makeText(OneActivity.this, "请选择商品", Toast.LENGTH_SHORT).show();
                    }

                } else if (num == 4) {
                    oneThree1.baocun();
                    mRG.check(R.id.one_rb_4);
                    mController.showFragment(4);
                    show();

                } else if (num == -1) {
                    finish();
                }

            }
            break;
            case R.id.zhuye: {

                startActivity(new Intent(OneActivity.this, MainActivity.class));
                finish();
            }
            break;
            case R.id.xiadan_shop_car: {
                udimg();

            }
            break;
            case R.id.xiadan_liji: {
                if (shopsize == null) {
                    Toast.makeText(OneActivity.this, "请选择商品大小", Toast.LENGTH_SHORT).show();
                } else if (shopColor == null) {
                    Toast.makeText(OneActivity.this, "请选择商品颜色", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(OneActivity.this, UserOrderXiaActivity.class));
                }

            }
            break;
            case R.id.xiadan_jia: {

                DecimalFormat df = new DecimalFormat("######0.00");
                mShopNum.setText(Integer.parseInt(mShopNum.getText().toString().trim()) + 1 + "");
                double d = Double.parseDouble(mShopNum.getText().toString().trim()) * Double.parseDouble(productTemplates.getPreferential_price());
                mShopjage.setText(df.format(d) + "");
                shopnum = mShopNum.getText().toString();
            }
            break;
            case R.id.xiadan_jian: {
                if (Integer.parseInt(mShopNum.getText().toString().trim()) == 1) {

                } else {
                    DecimalFormat df = new DecimalFormat("######0.00");
                    mShopNum.setText(Integer.parseInt(mShopNum.getText().toString().trim()) - 1 + "");
                    double d = Double.parseDouble(mShopNum.getText().toString().trim()) * Double.parseDouble(productTemplates.getPreferential_price());
                    mShopjage.setText(df.format(d) + "");
                    shopnum = mShopNum.getText().toString();
                }
            }
            break;
        }
    }

    public void show() {
        dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        inflate = LayoutInflater.from(this).inflate(R.layout.item_xiadan, null);
        //初始化控件
        dialogsezi();
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = getWindowManager().getDefaultDisplay().getWidth();
        lp.y = 20;//设置Dialog距离底部的距离
        //       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
    }

    private void dialogsezi() {
        HashMap<String, String> map = new HashMap<>();
        map.put("product_template_id", productTemplates.getProduct_temp_id());
        new MyUtils().xUtilsPost(URL.URL_shop_size, map, handler, ID_shop_size);
        new MyUtils().xUtilsPost(URL.URL_shop_color, map, handler, URL.ID_shop_color);
        mShopCar =   inflate.findViewById(R.id.xiadan_shop_car);
        mLiJI =  inflate.findViewById(R.id.xiadan_liji);
        mCiMa = ((RecyclerView) inflate.findViewById(R.id.xiadan_rl_cima));
        mColor = ((RecyclerView) inflate.findViewById(R.id.xiadan_rl_color));
        mShopJia = ((ImageView) inflate.findViewById(R.id.xiadan_jia));
        mShopJian = ((ImageView) inflate.findViewById(R.id.xiadan_jian));
        mShopNum = ((TextView) inflate.findViewById(R.id.xiadan_num));
        mShopname = ((TextView) inflate.findViewById(R.id.xiadan_name));
        mShopjage = ((TextView) inflate.findViewById(R.id.xiadan_jage));
        mShopname.setText(productTemplates.getProduct_temp_name());
        mShopNum.setText(1 + "");
        mShopJia.setOnClickListener(this);
        mShopJian.setOnClickListener(this);
        mShopCar.setOnClickListener(this);
        mLiJI.setOnClickListener(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mCiMa.setLayoutManager(linearLayoutManager);
        mCiMa.setHasFixedSize(true);
        ciMaAdapter = new CiMaAdapter();
        mCiMa.setAdapter(ciMaAdapter);
        ciMaAdapter.setOnItemClickLitener(new CiMaAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(String s) {
                shopsize = s;
            }
        });
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mColor.setLayoutManager(linearLayoutManager1);
        mColor.setHasFixedSize(true);
        colorAdapter = new ColorAdapter();
        mColor.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickLitener(new ColorAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(String s) {
                shopColor = s;

            }
        });

    }

    @Override
    public void onBackPressed() {
        num--;
        if (num == 0) {
            mRG.check(R.id.one_rb_1);
            mController.showFragment(0);
        } else if (num == 1) {
            mRG.check(R.id.one_rb_2);
            mController.showFragment(1);
        } else if (num == 2) {
            mRG.check(R.id.one_rb_3);
            mController.showFragment(2);
        } else if (num == 3) {
            if (productTemplates != null) {
                mController.showFragment(3);
            } else {
                num--;
                Toast.makeText(OneActivity.this, "请选择商品", Toast.LENGTH_SHORT).show();
            }

        } else if (num == 4) {
            oneThree1.baocun();
            mRG.check(R.id.one_rb_4);
            mController.showFragment(4);
            show();

        } else if (num == -1) {
            finish();
        }
    }

    private void udimg() {
        Log.i("111111111111111111", "udimg: " + sejiandshopURL);
        new MyUtils().xUutlisImg(URL.URL_img_up, sejiandshopURL, handler, URL.ID_img_up);
    }

    @Override
    protected void onResume() {
        super.onResume();
        oneThree1.GengHuan();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppCommond a) {
        if (!AppCommond.check(a)) return;
        for (String activityName : a.getAcNameList()) {
            if (activityName.equals(getClass().getSimpleName())) {
                unreg();
                Log.i("URL.genghuanURL", URL.genghuanURL);
                oneThree1.GengHuan();
                //    finish();
                break;
            }
        }
    }

    void unreg() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

}
